{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Date Features\n",
    "\n",
    "Useful classes to generate date features and add them to `TimeGPT`."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "#| hide\n",
    "from nbdev.showdoc import show_doc\n",
    "from nixtla.date_features import CountryHolidays, SpecialDates"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/markdown": [
       "---\n",
       "\n",
       "#### CountryHolidays\n",
       "\n",
       ">      CountryHolidays (countries:List[str])\n",
       "\n",
       "Given a list of countries, returns a dataframe with holidays for each country."
      ],
      "text/plain": [
       "---\n",
       "\n",
       "#### CountryHolidays\n",
       "\n",
       ">      CountryHolidays (countries:List[str])\n",
       "\n",
       "Given a list of countries, returns a dataframe with holidays for each country."
      ]
     },
     "execution_count": null,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#| echo: false\n",
    "show_doc(CountryHolidays, title_level=4)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>US_New Year's Day</th>\n",
       "      <th>US_Memorial Day</th>\n",
       "      <th>US_Independence Day</th>\n",
       "      <th>US_Labor Day</th>\n",
       "      <th>US_Veterans Day</th>\n",
       "      <th>US_Veterans Day (observed)</th>\n",
       "      <th>US_Thanksgiving</th>\n",
       "      <th>US_Christmas Day</th>\n",
       "      <th>US_Martin Luther King Jr. Day</th>\n",
       "      <th>US_Washington's Birthday</th>\n",
       "      <th>...</th>\n",
       "      <th>US_Juneteenth National Independence Day (observed)</th>\n",
       "      <th>US_Christmas Day (observed)</th>\n",
       "      <th>MX_Año Nuevo</th>\n",
       "      <th>MX_Día de la Constitución</th>\n",
       "      <th>MX_Natalicio de Benito Juárez</th>\n",
       "      <th>MX_Día del Trabajo</th>\n",
       "      <th>MX_Día de la Independencia</th>\n",
       "      <th>MX_Día de la Revolución</th>\n",
       "      <th>MX_Transmisión del Poder Ejecutivo Federal</th>\n",
       "      <th>MX_Navidad</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>2018-09-03</th>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>...</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2018-09-04</th>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>...</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2018-09-05</th>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>...</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2018-09-06</th>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>...</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2018-09-07</th>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>...</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>5 rows × 24 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "            US_New Year's Day  US_Memorial Day  US_Independence Day  \\\n",
       "2018-09-03                  0                0                    0   \n",
       "2018-09-04                  0                0                    0   \n",
       "2018-09-05                  0                0                    0   \n",
       "2018-09-06                  0                0                    0   \n",
       "2018-09-07                  0                0                    0   \n",
       "\n",
       "            US_Labor Day  US_Veterans Day  US_Veterans Day (observed)  \\\n",
       "2018-09-03             1                0                           0   \n",
       "2018-09-04             0                0                           0   \n",
       "2018-09-05             0                0                           0   \n",
       "2018-09-06             0                0                           0   \n",
       "2018-09-07             0                0                           0   \n",
       "\n",
       "            US_Thanksgiving  US_Christmas Day  US_Martin Luther King Jr. Day  \\\n",
       "2018-09-03                0                 0                              0   \n",
       "2018-09-04                0                 0                              0   \n",
       "2018-09-05                0                 0                              0   \n",
       "2018-09-06                0                 0                              0   \n",
       "2018-09-07                0                 0                              0   \n",
       "\n",
       "            US_Washington's Birthday  ...  \\\n",
       "2018-09-03                         0  ...   \n",
       "2018-09-04                         0  ...   \n",
       "2018-09-05                         0  ...   \n",
       "2018-09-06                         0  ...   \n",
       "2018-09-07                         0  ...   \n",
       "\n",
       "            US_Juneteenth National Independence Day (observed)  \\\n",
       "2018-09-03                                                  0    \n",
       "2018-09-04                                                  0    \n",
       "2018-09-05                                                  0    \n",
       "2018-09-06                                                  0    \n",
       "2018-09-07                                                  0    \n",
       "\n",
       "            US_Christmas Day (observed)  MX_Año Nuevo  \\\n",
       "2018-09-03                            0             0   \n",
       "2018-09-04                            0             0   \n",
       "2018-09-05                            0             0   \n",
       "2018-09-06                            0             0   \n",
       "2018-09-07                            0             0   \n",
       "\n",
       "            MX_Día de la Constitución  MX_Natalicio de Benito Juárez  \\\n",
       "2018-09-03                          0                              0   \n",
       "2018-09-04                          0                              0   \n",
       "2018-09-05                          0                              0   \n",
       "2018-09-06                          0                              0   \n",
       "2018-09-07                          0                              0   \n",
       "\n",
       "            MX_Día del Trabajo  MX_Día de la Independencia  \\\n",
       "2018-09-03                   0                           0   \n",
       "2018-09-04                   0                           0   \n",
       "2018-09-05                   0                           0   \n",
       "2018-09-06                   0                           0   \n",
       "2018-09-07                   0                           0   \n",
       "\n",
       "            MX_Día de la Revolución  \\\n",
       "2018-09-03                        0   \n",
       "2018-09-04                        0   \n",
       "2018-09-05                        0   \n",
       "2018-09-06                        0   \n",
       "2018-09-07                        0   \n",
       "\n",
       "            MX_Transmisión del Poder Ejecutivo Federal  MX_Navidad  \n",
       "2018-09-03                                           0           0  \n",
       "2018-09-04                                           0           0  \n",
       "2018-09-05                                           0           0  \n",
       "2018-09-06                                           0           0  \n",
       "2018-09-07                                           0           0  \n",
       "\n",
       "[5 rows x 24 columns]"
      ]
     },
     "execution_count": null,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "import pandas as pd\n",
    "\n",
    "c_holidays = CountryHolidays(countries=['US', 'MX'])\n",
    "periods = 365 * 5\n",
    "dates = pd.date_range(end='2023-09-01', periods=periods)\n",
    "holidays_df = c_holidays(dates)\n",
    "holidays_df.head()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/markdown": [
       "---\n",
       "\n",
       "#### SpecialDates\n",
       "\n",
       ">      SpecialDates (special_dates:Dict[str,List[str]])\n",
       "\n",
       "Given a dictionary of categories and dates, returns a dataframe with the special dates."
      ],
      "text/plain": [
       "---\n",
       "\n",
       "#### SpecialDates\n",
       "\n",
       ">      SpecialDates (special_dates:Dict[str,List[str]])\n",
       "\n",
       "Given a dictionary of categories and dates, returns a dataframe with the special dates."
      ]
     },
     "execution_count": null,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#| echo: false\n",
    "show_doc(SpecialDates, title_level=4)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>Important Dates</th>\n",
       "      <th>Very Important Dates</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>2018-09-03</th>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2018-09-04</th>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2018-09-05</th>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2018-09-06</th>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2018-09-07</th>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "            Important Dates  Very Important Dates\n",
       "2018-09-03                0                     0\n",
       "2018-09-04                0                     0\n",
       "2018-09-05                0                     0\n",
       "2018-09-06                0                     0\n",
       "2018-09-07                0                     0"
      ]
     },
     "execution_count": null,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "special_dates = SpecialDates(\n",
    "    special_dates={\n",
    "        'Important Dates': ['2021-02-26', '2020-02-26'],\n",
    "        'Very Important Dates': ['2021-01-26', '2020-01-26', '2019-01-26']\n",
    "    }\n",
    ")\n",
    "periods = 365 * 5\n",
    "dates = pd.date_range(end='2023-09-01', periods=periods)\n",
    "holidays_df = special_dates(dates)\n",
    "holidays_df.head()"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "python3",
   "language": "python",
   "name": "python3"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
